$(function(){
	   easyloader.base = 'js/tools/easyui/';
	   using("locale/easyui-lang-zh_CN.js",function(){
	      var columns=[[
	                              {field: 'name',title: '名称',width: 80,align: 'center',hidden:true},
						          {field: 'columnId',title: '名称',width: 80,align: 'center'},
						          {field: 'delete',title: '删除',width: 80,align: 'center',editor:'numberbox'}
							 ]];
	        
			 
			 $("#dataGrid").treegrid({
				nowrap: false,
				rownumbers: true,
				collapsible:true,
				height:560,
				singleSelect:true,
				queryParams:{"category.columnId":$.query.get("columnId")},
				url:'category!categoryList.action',
				idField:'name',
				treeField:'name',
				frozenColumns:[[
	                {
						title: '名称',
						field: 'name',
						width: 150
					}
				]],
				columns:[[
				{field:'sort',title:'排序',width:50,rowspan:2},
				    {field:'opt',title:'操作',width:150,rowspan:2}
				]]
			}); 
			
		
	   });
	   $("#dialog").dialog({title:"添加类别"});
});
//添加类别
function addWindow()
{
	setTile('添加类别');
	$("#add").show();
	$("#modify").hide();
	$("#sort").val(1);
	$("#pname").val("无");
	$("#name").val("");
	$('#dialog').dialog('open');
}
function addCategory()
{
	var name=$.trim($("#name").val());
	var columnId=$.query.get("columnId");
	var sort=$.trim($("#sort").val());
	if(name.length<1||name.length>20)
	{
		alert("名称在1~20之间！");
		$("#name").css({"border": "2px solid red"});
		return false;
	}
	var reg=/^[0-9]+$/;
		if(!reg.test(sort))
		{
			 alert("必需输入数字");
			  $("#sort").css({"border": "2px solid red"});
			  return false;
		}else
		{
			 $("#sort").css({"border": ""});
		}
	var param={};
	    param["category.name"]=encodeURI(name);
		param["category.columnId"]=columnId;
		param["category.sort"]=sort;
		param["category.pid"]=pid;
	$.getJSON("category!addCategory.action",param,function(data){
		if(data.info)
		{
			$('#dataGrid').treegrid("reload");
			 $("#dialog").dialog("close");
			alert("添加成功!");
		}else
		{
			alert("添加失败!");
		}
	});
}
//删除用户
function deleteCategory(id)
{
	$.getJSON("category!deleteCategory.action",{"category.id":id},function(data){
		       if(data.info)
					{
						$('#dataGrid').treegrid("reload");
						alert("删除成功");
					}else
					{
						alert("删除失败");
					}
	});
}
//设置标题
function setTile(title)
{
	$("#dialog").dialog("setTitle",title);
}
//修改
var categoryId;
function modify(id,name,pname,sort){
		
	categoryId=id;
	setTile("修改类别");
	$("#pname").val(pname);
	$("#add").hide();
	$("#modify").show();
	$("#sort").val(sort);
	$("#name").val(name);
	$('#dialog').dialog('open');
	
}
function modifyCategory()
{
	var name=$.trim($("#name").val());
	var sort=$.trim($("#sort").val());
	if(name.length<1||name.length>20)
	{
		alert("名称在1~20之间！");
		return false;
	}
	var reg=/^[0-9]+$/;
		if(!reg.test(sort))
		{
			 alert("必需输入数字");
			  $("#sort").css({"border": "2px solid red"});
			  return false;
		}else
		{
			 $("#sort").css({"border": ""});
		}
	var param={};
	    param["category.id"]=categoryId;
	    param["category.name"]=encodeURI(name);
		param["category.sort"]=sort;
	$.getJSON("category!modifyCategory.action",param,function(data){
		if(data.info)
		{
			$('#dataGrid').treegrid("reload");
			$('#dialog').dialog('close');
			alert("修改成功!");
		}else
		{
			alert("修改失败!");
		}
	});
}
var pid=-1;
//添加子类别
function append(id,columnId,pname)
{
	pid=id;
	setTile("添加子类别");
	$("#add").show();
	$("#modify").hide();
	$("#pname").val(pname);
	$("#name").val('');
	$("#sort").val(1);
	$('#dialog').dialog('open');
}
